home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdev8bcm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  2.5 KB  |  83 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdev8bcm.c,v 1.2 2000/09/19 19:00:11 lpd Exp $ */
  20. /* Dynamic color mapping for 8-bit displays */
  21. #include "gx.h"
  22. #include "gxdevice.h"
  23. #include "gdev8bcm.h"
  24.  
  25. /* Initialize an 8-bit color map. */
  26. void
  27. gx_8bit_map_init(gx_8bit_color_map * pcm, int max_count)
  28. {
  29.     int i;
  30.  
  31.     pcm->count = 0;
  32.     pcm->max_count = max_count;
  33.     for (i = 0; i < gx_8bit_map_size; i++)
  34.     pcm->map[i].rgb = gx_8bit_no_rgb;
  35. }
  36.  
  37. /* Look up a color in an 8-bit color map. */
  38. /* Return <0 if not found. */
  39. int
  40. gx_8bit_map_rgb_color(const gx_8bit_color_map * pcm, gx_color_value r,
  41.               gx_color_value g, gx_color_value b)
  42. {
  43.     ushort rgb = gx_8bit_rgb_key(r, g, b);
  44.     const gx_8bit_map_entry *pme =
  45.     &pcm->map[(rgb * gx_8bit_map_spreader) % gx_8bit_map_size];
  46.  
  47.     for (;; pme++) {
  48.     if (pme->rgb == rgb)
  49.         return pme->index;
  50.     else if (pme->rgb == gx_8bit_no_rgb)
  51.         break;
  52.     }
  53.     if (pme != &pcm->map[gx_8bit_map_size])
  54.     return pme - &pcm->map[gx_8bit_map_size];
  55.     /* We ran off the end; wrap around and continue. */
  56.     pme = &pcm->map[0];
  57.     for (;; pme++) {
  58.     if (pme->rgb == rgb)
  59.         return pme->index;
  60.     else if (pme->rgb == gx_8bit_no_rgb)
  61.         return pme - &pcm->map[gx_8bit_map_size];
  62.     }
  63. }
  64.  
  65. /* Add a color to an 8-bit color map after an unsuccessful lookup, */
  66. /* and return its index.  Return <0 if the map is full. */
  67. int
  68. gx_8bit_add_rgb_color(gx_8bit_color_map * pcm, gx_color_value r,
  69.               gx_color_value g, gx_color_value b)
  70. {
  71.     int index;
  72.     gx_8bit_map_entry *pme;
  73.  
  74.     if (gx_8bit_map_is_full(pcm))
  75.     return -1;
  76.     index = gx_8bit_map_rgb_color(pcm, r, g, b);
  77.     if (index >= 0)        /* shouldn't happen */
  78.     return index;
  79.     pme = &pcm->map[-index];
  80.     pme->rgb = gx_8bit_rgb_key(r, g, b);
  81.     return (pme->index = pcm->count++);
  82. }
  83.